package com.dhy.servlet;

import java.io.IOException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.dhy.service.AlipayService;

import Utils.JsonUtils;
import entity.Hotel;
import entity.Payment;
import entity.Price;
@WebServlet("/post")
public class AlipayServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private AlipayService alipayService = new AlipayService(); 
	List<Object> list = new ArrayList<Object>();
	private JsonUtils jsonUtils = new JsonUtils();
	private JSONObject json = new JSONObject();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		String methodName = request.getMethod();
		System.out.println("doGet---->"+methodName);
		if ("showPayment".equals(method)) {
			showPayment(request, response);
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String methodName = request.getMethod();
		System.out.println("doPost---->"+methodName);
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String methodName = request.getMethod();
		System.out.println("service---->"+methodName);
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		if ("GET".equals(methodName)) {
			doGet(request, response);
		} else if ("POST".equals(methodName)) {
			doPost(request, response);
		}
	}
	
/**
 * 开始支付
 * @param request
 * @param response
 * @throws IOException 
 * @throws ServletException 
 */
	private void showPayment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int price_id = Integer.parseInt(request.getParameter("id"));
		int zc_user_id = Integer.parseInt(request.getParameter("zc_user_id"));
		System.out.println("price_id--->"+price_id);
		System.out.println("zc_user_id--->"+zc_user_id);
		Payment payment = new Payment();
		// 订单编号
		String number = request.getParameter("pay_number");
		System.out.println("支付时的订单编号--->"+number);
		// 当前时间
		String create_time = getSystemDate();
		System.out.println("当前时间--->"+create_time);
		
		// 查询price --->price_id
		Price price = alipayService.queryPrice(price_id);
		int hotel_id = price.getHotel_id();
		// 查询hotel --->price_id
		Hotel hotel = alipayService.querHotel(hotel_id);
		
		payment.setNumber(number);
		payment.setPrice(price.getSum_price());
		payment.setTxt(hotel.getHotel_text());
		payment.setHotel_id(hotel_id);
		payment.setCreate_time(create_time);
		payment.setStatus("N");
		payment.setPerson_id(price.getPerson_id());
		payment.setUser_id(zc_user_id);
		//new Payment();
		
		int i  = alipayService.addPayment(payment);
		if(i==1) {
			System.out.println("payment增加成功...");
		}
		// 存到session域中
		request.getSession().setAttribute("payment", payment);
		json.fluentPut("code", 1);
		jsonUtils.send(response, json);
	}

	
	/**
	 * 产生订单号
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public String getrandomNum() {
		Date vNow = new Date();
		String sNow = "";
		sNow += (vNow.getYear()-100);
		sNow += (vNow.getMonth() + 1);
		sNow += vNow.getDate();
		sNow += vNow.getHours();
		sNow += vNow.getMinutes();
		sNow += vNow.getSeconds();
		sNow += new Random().nextInt(10);
		sNow += new Random().nextInt(10);
		sNow += new Random().nextInt(10);
		sNow += new Random().nextInt(10);
		return sNow;
	}
	/**
	 * 获取当前年月日时分秒
	 * @throws ParseException 
	 */
	public String getSystemDate(){
		//转换提日期输出格式
		return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
	}
}
